package com.kly.sc.api.application.controller.user;

import cn.hutool.core.date.DateUtil;
import com.amazonaws.services.dynamodbv2.xspec.S;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.kly.sc.api.application.constant.ApiRedisKeys;
import com.kly.sc.api.application.enums.ApiResultStatus;
import com.kly.sc.api.application.service.utils.BaseComponent;
import com.kly.user.dto.chatter.request.PrivateMessageReq;
import com.kly.user.enums.RememberTime;
import com.kly.user.enums.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("/api/albums")
public class UserAlbumsController {

    @Resource
    RedisClient redisClient;
    @Resource
    BaseComponent baseComponent;

    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final Long ONE = 1L;

    /**
     * 发送私密相册信息
     * @param messageReq messageReq.fromUserId  userCode
     *                   messageReq.toUserId    会话对方的userCode
     *                   messageReq.opType      发送类型，1、图片 2、视频
     *                   messageReq.albumCode   相册code
     *                   messageReq.imageCode   图片code
     */
    @ResponseBody
    @RequestMapping(value = "/sendAlbums")
    public Response sendPrivateAlbums(@Valid @RequestBody PrivateMessageReq messageReq, BindingResult results) {
        //此接口通过回调完成，不需要前端单独调用
        if (results.hasErrors()) {
            return Response.error(ApiResultStatus.PARAMETER_VALIDATION_FAILED);
        }
        String dateTime = DateUtil.format(DateUtil.offsetDay(new Date(), ONE.intValue()), FORMAT);
        //天际未查看图片记录
        redisClient.hPut(ApiRedisKeys.NOT_VIEWED_PRIVATE_ALBUM.getValue(),
                messageReq.getImageCode(), dateTime, messageReq.getFromUserId(), messageReq.getToUserId());
        //设置过期时间为1天
        redisClient.expire(ApiRedisKeys.NOT_VIEWED_PRIVATE_ALBUM.getValue(), (int)RememberTime.ONE_DAY.getTime(),
                messageReq.getFromUserId(), messageReq.getToUserId());

        return Response.ok();

    }
}
